﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using InTheHand.Net.Sockets;
using Newtonsoft.Json.Linq;
using WinHBM.Core.Bluetooth;
using WinHBM.Core.Net.Http;
using WinHBM.Utils;

namespace WinHBM.Core.Net.Sockets.Cmd
{
    public class BluetoothDataCmd : SocketCmd
    {
        private HttpClient httpClient = HttpClient.Instance;
        private static readonly log4net.ILog Logger = log4net.LogManager.GetLogger(typeof(BluetoothDataCmd));

        public override void executeCmd(JObject request, FlashSocketServer socket)
        {

            if(BluetoothClientHolder.bluetoothClient == null)
            {
                socket.Send(wrapCmd(CMD_BLUETOOTH_DATA, "未连接"));
                return;
            }

            try
            {
                string bluetoothCmd = request["bluetoothCmd"].ToString();
                MagicBar bar = new MagicBar(BluetoothClientHolder.bluetoothClient);
                JObject data = new JObject();
                Console.WriteLine("BluetoothDataCmd :"+bluetoothCmd);
                if (bluetoothCmd.Equals("BATTERY"))
                {
                    int battery = bar.CMD_GetBattery();
                    data["bluetoothCmd"] = "BATTERY";
                    data["battery"] = battery.ToString();
                    Console.WriteLine("BluetoothDataCmd 电量:" + battery);
                    socket.Send(wrapCmd(CMD_BLUETOOTH_DATA, data));
                }
                else if (bluetoothCmd.Equals("FLOWER_COUNT"))
                {
                    data["bluetoothCmd"] = "FLOWER_COUNT";
                    int count = bar.CMD_GetCacheCount();
                    data["count"] = count.ToString();
                    socket.Send(wrapCmd(CMD_BLUETOOTH_DATA, data));
                }
                else if (bluetoothCmd.Equals("FLOWER_DATA"))
                {
                    data["bluetoothCmd"] = "FLOWER_DATA";
                    int count = bar.CMD_GetCacheCount();
                    if (count > 0)
                    {
                        RedFlower flower = bar.CMD_GetRedData();
                        data["sender_user_role_id"] = flower.teacherId;
                        data["baby_id"] = flower.studentId;
                        data["flower_time"] = flower.year + "-" + flower.month + "-" + flower.day + " " + flower.hour + ":" + flower.minute + ":" + flower.second;
                        if (flower.type == FlowerType.Red)
                        {
                            data["type"] = "0";
                        }
                        else if (flower.type == FlowerType.Gold)
                        {
                            data["type"] = "1";
                        }
                        Logger.Debug(data.ToString());
                        //数据库保存红花记录
                        UtilFlower.localSave(flower);
                        data["flower"] = count - 1;
                        data["flower_status"] = true;
                        socket.Send(wrapCmd(CMD_BLUETOOTH_DATA, data));
                    }
                    else
                    {
                        Console.WriteLine("无红花数据" + count);
                        data["flower"] = -1;
                        data["flower_status"] = true;
                        socket.Send(wrapCmd(CMD_BLUETOOTH_DATA, data));
                    }

                }
                else if (bluetoothCmd.Equals("UPDATE_DATETIME"))
                {
                    int teacherId = bar.CMD_ReadTeacherId();
                    data["teacherId"] = teacherId.ToString();
                }
                else if (bluetoothCmd.Equals("WRITE_TEACHER_ID"))
                {
                    int teacherId = bar.CMD_ReadTeacherId();
                    data["teacherId"] = teacherId.ToString();
                }
                else if (bluetoothCmd.Equals("WRITE_CARD_ID"))
                {
                    int teacherId = bar.CMD_ReadTeacherId();
                    data["teacherId"] = teacherId.ToString();
                }
            }
            catch (Exception e)
            {
                Logger.Error(e);
                socket.Send(wrapCmd(CMD_BLUETOOTH_DATA, e.Message));
            }
            
            
        }
    }
}
